package com.witte.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.witte.entity.CusNOrdProN;
import com.witte.entity.Customer;
import com.witte.entity.RespBean;
import com.witte.entity.Statitic;
import com.witte.service.CusService;
import com.witte.service.OrdersService;
import com.witte.service.ServiceService;

@Controller
@ResponseBody
@RequestMapping(value="/api/Statistic",produces="application/json;charset=UTF-8")
@CrossOrigin
public class StatisticController {

	@Autowired
	private CusService cusService;
	
	@Autowired
	private OrdersService ordersService;
	
	@Autowired
	ServiceService serviceService;


	//this methods only return two lists (count,level)
	@PostMapping(value = "/findAllCusByLevel")
	public RespBean selectLevel() {
		System.out.println(cusService.selectAllCusByLevel());
		RespBean respBean = RespBean.ok(null, cusService.selectAllCusByLevel());
		return respBean;
	}

	//this methods only return two lists (count,satisfy)
	@PostMapping(value = "/findAllCusByStatisfy")
	public RespBean selectStatisfy() {
		System.out.println(cusService.selectAllCusByStatify());
		RespBean respBean = RespBean.ok(null, cusService.selectAllCusByStatify());
		return respBean;
	}

	//this methods only return two lists (count,credit)
	@PostMapping(value = "/findAllCusByCredit")
	public RespBean selectCredit() {
		System.out.println(cusService.selectAllCusByCredit());
		RespBean respBean = RespBean.ok(null, cusService.selectAllCusByCredit());
		return respBean;
	}
	
	@PostMapping(value = "/getSumMoney")
	public RespBean getSumMoney() {
		//System.out.println(ordersService.getSumMoney().getCount());
		int st = ordersService.getSumMoney();
		System.out.println(st);
		RespBean respBean = RespBean.ok(null, st);
		return respBean;
	}
	
	@PostMapping(value = "/getAllCOP")
	public RespBean getAllCOP() {
		//System.out.println(ordersService.getSumMoney().getCount());
		List<CusNOrdProN> cop = ordersService.getAllCOP();
		System.out.println(cop.size());
		RespBean respBean = RespBean.ok(null, cop);
		return respBean;
	}
	
	
	@PostMapping(value = "/getAllInfoByName")
	public RespBean getAllInfoByName(String name) {
		//System.out.println(ordersService.getSumMoney().getCount());
		List<CusNOrdProN> cop = ordersService.getCOPByCusName(name);
		System.out.println(cop.size());
		RespBean respBean = RespBean.ok(null, cop);
		return respBean;
	}
	
	@PostMapping(value = "/getSumByType")
	public RespBean getSumByType() {
		//System.out.println(ordersService.getSumMoney().getCount());
		List<Object> xList = new ArrayList<>();
		List<Object> yList = new ArrayList<>();
		for(Statitic i:serviceService.getSumByType()) {
			yList.add(i.getCount());
			xList.add(i.getLevelName());
		}
		Map<String,List<Object>> ret = new HashMap<>();
		ret.put("x", xList);
		ret.put("y", yList);
		RespBean respBean = RespBean.ok(null, ret);
		return respBean;
	}
	
	@PostMapping(value = "/getLostCus")
	public RespBean getLostCus() {
		//System.out.println(ordersService.getSumMoney().getCount());
		List<Integer> lost = ordersService.getAllLost();
		List<String> lostName = new ArrayList();
		for(int i:lost) {
			Integer temp = i;
			lostName.add(cusService.selectNameByNo(temp));
		}
		Map<String,List<String>> ret = new HashMap<>();
		ret.put("x", lostName);
		RespBean respBean = RespBean.ok(null, ret);
		return respBean;
	}
}
